//
//  GoodInfoTableViewCell.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/4/5.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "GoodInfoTableViewCell.h"
#import "GoodInfoCollectionViewCell.h"
#import "BrowPhotoView.h"

@interface GoodInfoTableViewCell()<UICollectionViewDelegate , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UIView *titleView;
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UILabel *infoLabel;
Strong UICollectionView *collectionView;
Strong UIView *bgView;

@end

@implementation GoodInfoTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = 0;
        self.separatorInset = UIEdgeInsetsZero;
        if ([self respondsToSelector:@selector(setLayoutMargins:)]) {
            self.layoutMargins = UIEdgeInsetsZero;
        }
        
        _titleView = [UIView new];
        _titleView.backgroundColor = [UIColor whiteColor];
        
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];

        _titleLabel = [UILabel new];
        _titleLabel.font = DEF_FontSize_14;
        
        NSMutableAttributedString *attStr = [NSMutableAttributedString new];
        NSTextAttachment *imgAtt = [NSTextAttachment new];
        imgAtt.image = [UIImage imageNamed:@"info"];
        imgAtt.bounds = CGRectMake(0, -2, 15, 15);
        [attStr appendAttributedString:[NSAttributedString attributedStringWithAttachment:imgAtt]];
        [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:@" 商品详情"]];
        _titleLabel.attributedText = attStr;
        
        _infoLabel = [UILabel new];
        _infoLabel.font = DEF_FontSize_11;
        _infoLabel.numberOfLines = 0;
        
        self.contentView.backgroundColor = getColor(@"f1f2f6");
        [self.contentView addSubview:_titleView];
        [_titleView addSubview:_titleLabel];
        [self.contentView addSubview:_bgView];
        
        [_bgView sd_addSubviews:@[_infoLabel  , self.collectionView]];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    _titleView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 27);
    _bgView.frame = CGRectMake(0, 27 + 5, SCREEN_WIDTH, 30 + SCREEN_WIDTH / 4 * self.imgArray.count + self.imgArray.count * 5 + 300);
    _titleLabel.frame = CGRectMake(10, 0, SCREEN_WIDTH-20, 27);
    _infoLabel.frame = CGRectMake(10, 0, SCREEN_WIDTH-20, 30);
    _collectionView.frame = CGRectMake(0, 30, SCREEN_WIDTH , SCREEN_WIDTH / 4 * self.imgArray.count + self.imgArray.count * 5);
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        flowLayout.minimumInteritemSpacing = 5;
        flowLayout.minimumLineSpacing = 5;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH , SCREEN_WIDTH / 4 * self.imgArray.count) collectionViewLayout:flowLayout];
        [_collectionView registerClass:[GoodInfoCollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionView"];
        _collectionView.backgroundColor = getColor(@"ffffff");
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imgArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GoodInfoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionView" forIndexPath:indexPath];
    cell.model = self.imgArray[indexPath.row];
    return cell;
}

#pragma mark -  UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [[BrowPhotoView alloc]initWithFrame:self.bounds array:self.imgArray selectIndex:indexPath.row];
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH / 4);
}

- (void)setDataDictionary:(NSDictionary *)dataDictionary{
    _dataDictionary = dataDictionary;
    _infoLabel.text = dataDictionary[@"c_introduce"];
}

- (void)setImgArray:(NSArray *)imgArray{
    _imgArray = imgArray;
    [self.collectionView reloadData];
}

@end
